package com.lijiajia.cloud.resource.common.web;

import com.lijiajia.cloud.common.security.resolver.CloudUserResolver;
import com.lijiajia.cloud.resource.common.web.servlet.RestReturnHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.Charset;
import java.util.List;

/**
 * 默认ServletWeb配置
 *
 * @author lijiajia
 * @since 2019-04-11
 */
@Slf4j
public class BaseWebMvcConfiguration extends WebMvcConfigurationSupport {

	@Override
	protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
		log.debug("cloud configure return handler");
		returnValueHandlers.add(new RestReturnHandler());
	}

	@Override
	protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
		argumentResolvers.add(new CloudUserResolver());
	}

	@Override
	protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		log.debug("cloud configure web message converter");
	}

	@Override
	protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		log.debug("cloud configure web default message converter");
		converters.forEach(converter -> {
			if (converter instanceof StringHttpMessageConverter) {
				((StringHttpMessageConverter) converter).setDefaultCharset(Charset.defaultCharset());
			}
		});
	}
}
